/*Copyright 2014 Rajesh Putta http://www.codesnippets4all.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* 
*/

package com.codesnippets4all.jthunder.extension.plugins.input.xpath;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import com.codesnippets4all.jthunder.core.IPlugin;
import com.codesnippets4all.jthunder.core.exceptions.AutomationException;
import com.codesnippets4all.jthunder.core.execution.IStatus;
import com.codesnippets4all.jthunder.core.execution.Status;
import com.codesnippets4all.jthunder.core.execution.StatusType;
import com.codesnippets4all.jthunder.core.execution.status.AutomationStatusManager;
import com.codesnippets4all.jthunder.core.state.IContext;
import com.codesnippets4all.jthunder.core.utils.Utility;

public class XpathInputPlugin implements IPlugin {

	public IStatus process(Map<String, String> configuration, IContext context,
			AutomationStatusManager statusManager) {
		
		String filepath = configuration.get("filepath");
		
		if(filepath==null || filepath.trim().equals(""))
		{
			throw new AutomationException("XpathInputPlugin component requires 'filepath' to be configured in task configuration...");
		}
		
		FileInputStream iStream=null;
		Map<String, String> xpathResultsMap=null;
		
		try{
			iStream=new FileInputStream(filepath);

			
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
			
			DocumentBuilder builder=factory.newDocumentBuilder();
			
			Document document=builder.parse(iStream);
			
			XPathFactory xpathFactory=XPathFactory.newInstance();
			
			XPath xpath=xpathFactory.newXPath();
			
			
			Set<Entry<String, String>> entrySet=configuration.entrySet();
			
			xpathResultsMap=new HashMap<String, String>();
			
			for(Entry<String, String> entry: entrySet)
			{
				String key=entry.getKey();
				
				if(!key.startsWith("xpath_"))
					continue;
				
				String value=entry.getValue();
				
				XPathExpression expression=xpath.compile(value);
				
				String result=expression.evaluate(document);
				
				xpathResultsMap.put(key, result);
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new AutomationException(e);
		}
		finally{
			Utility.closeStream(iStream);
		}
		
		IStatus status=new Status(StatusType.SUCCESS, xpathResultsMap);
		
		return status;
		
	}
}
